package com.tju.resource.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tju.resource.common.ServiceException;
import com.tju.resource.mapper.TbMechanicalMapper;
import com.tju.resource.mapper.TbProdPlanMapper;
import com.tju.resource.model.TbMechanical;
import com.tju.resource.model.TbProdPlan;
import com.tju.resource.server.TbMechanicalService;
import com.tju.resource.tool.result.Result;
import com.tju.resource.tool.result.ResultEnum;
import com.tju.resource.tool.result.ResultUtil;
import com.tju.resource.tool.tool.ChineseExceptionUtils;
import com.tju.resource.tool.tool.ClientParameters;
import com.tju.resource.tool.tool.EmptyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理实现类
 */
@Slf4j
@Service
public class TbMechanicalServiceImpl implements TbMechanicalService {

    @Autowired
    private TbMechanicalMapper tbMechanicalMapper;
    @Autowired
    private TbProdPlanMapper tbProdPlanMapper;
    @Autowired
    private ChineseExceptionUtils zh_CH;

    /**
     * 添加设备信息
     *
     * @param tbMechanical
     * @return
     */
    @Transactional
    @Override
    public Result<Object> add(TbMechanical tbMechanical) {
        try {
            QueryWrapper<TbMechanical> wrapper = new QueryWrapper<>();
            wrapper.eq("mech_no", tbMechanical.getMechNo());
            if (EmptyUtils.isEmpty(tbMechanicalMapper.selectOne(wrapper))) {
                if (tbMechanicalMapper.insert(tbMechanical) > 0) {
                    return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.AddedSuccessfully);
                }
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "设备编号已占用。");
        } catch (Exception e) {
            throw new ServiceException(zh_CH.AddFailed);
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.AddFailed);
    }

    /**
     * 删除设备信息
     *
     * @param tbMechanical
     * @return
     */
    @Transactional
    @Override
    public Result<Object> del(TbMechanical tbMechanical) {
        try {
            QueryWrapper<TbProdPlan> wrapper = new QueryWrapper<>();
            wrapper.eq("mech_id", tbMechanical.getId());
            if (EmptyUtils.isEmpty(tbProdPlanMapper.selectList(wrapper))) {
                if (EmptyUtils.isNotEmpty(tbMechanical.getId())) {
                    if (tbMechanicalMapper.deleteById(tbMechanical.getId()) > 0)
                        return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyDeleted);
                } else
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.MissingParameters);
            } else
                return ResultUtil.error(ResultEnum.UNKONW_ERROR, "此设备已在生产计划中使用，无法删除！");
        } catch (Exception e) {
            throw new SecurityException(zh_CH.FailedToDelete);
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailedToDelete);
    }

    /**
     * 修改设备信息
     *
     * @param tbMechanical
     * @return
     */
    @Transactional
    @Override
    public Result<Object> upd(TbMechanical tbMechanical) {
        try {
            QueryWrapper<TbMechanical> wrapper = new QueryWrapper<>();
            wrapper.eq("mech_no", tbMechanical.getMechNo());
            TbMechanical mechanical = tbMechanicalMapper.selectOne(wrapper);
            if (EmptyUtils.isNotEmpty(mechanical)) {
                if (!mechanical.getId().equals(tbMechanical.getId())) {
                    return ResultUtil.error(ResultEnum.UNKONW_ERROR, "设备编号已占用。");
                }
            }
            if (tbMechanicalMapper.updateById(tbMechanical) > 0) {
                return ResultUtil.success(ResultEnum.SUCCESS, zh_CH.SuccessfullyModified);
            }
        } catch (Exception e) {
            throw new ServiceException(zh_CH.FailToEdit);
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR, zh_CH.FailToEdit);
    }

    /**
     * 分页查询设备信息
     *
     * @return
     */
    @Override
    public Result<Object> get(ClientParameters parameter) {
        try {
            QueryWrapper<TbMechanical> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("id");
            if (EmptyUtils.isNotEmpty(parameter.getOne())) {/*设备编号*/
                wrapper.eq("mech_no", parameter.getOne());
            }
            if (EmptyUtils.isNotEmpty(parameter.getTwo())) {/*设备名称(模糊)*/
                wrapper.like("mech_name", parameter.getTwo());
            }
            if (EmptyUtils.isNotEmpty(parameter.getPage()) && EmptyUtils.isNotEmpty(parameter.getLimit())) {
                Page<TbMechanical> page = new Page<>(parameter.getPage(), parameter.getLimit());
                IPage<TbMechanical> iPage = tbMechanicalMapper.selectPage(page, wrapper);
                return ResultUtil.success(ResultEnum.SUCCESS, iPage.getRecords(), iPage.getTotal());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResultUtil.error(ResultEnum.UNKONW_ERROR);
    }
}
